/**
 * Created by vincent on 2016/12/5.
 */
moduleMain.controller('MenuController', function (DialogService, LanguageService, GeneralResource, $rootScope, $scope, $state, $log, $filter, ConstantService, LoadingService) {
        initPage($scope);
        $scope.user = ConstantService.getItem('user');

        $scope.$on("$viewContentLoaded", function () {
            $scope.loadLanguage();

        });

        $rootScope.stateTree = ConstantService.getItem("stateTree");
        $scope.currentUser = ConstantService.getItem("user");

        $scope.booleanOptions = Enums.BooleanEnum.options;
        $scope.activeTag = ($state.current.name == "sz.firmOrder");
        $scope.getMenuTag = function () {
            return true;
        };

        $scope.openVersion = function () {
            DialogService.open("version", $scope);
        }

        $scope.jumpHome = function () {
            $rootScope.stateTree = [];
            $state.go("sz.homepage");
        };

        $scope.jumpState = function (state) {
            ConstantService.addState(state);
            $state.go(state.name);
        };

        $scope.logout = function () {
            templateActions.load = false;
            Alert.confirm(LanguageService.language('是否确认退出系统?'), function () {
                ConstantService.clear();
                $state.go("login");
            });
        };

        // ======================================================================================================================

        $scope.changeLanguage = function (language) {
            Alert.confirm(function () {
                LanguageService.changeLanguage(language.locale);
            })

        }

        $scope.loadLanguage = function () {
            var languageQueryPara = {
                className: Models.Language.className,
                all: true,
                query: {
                    projectName: {
                        field: Models.LanguageTemplate.query.projectName.name,
                        enumClass: Models.LanguageTemplate.query.projectName.enumClass,
                        value: 'WEB_ENTERPRISE'
                    }
                }
            };
            GeneralResource.query({}, languageQueryPara, function (queryResult) {
                $log.debug("查询结果", queryResult.data);
                $scope.languages = queryResult.data;
            });
        }

        // ======================================================================================================================

        $scope.showRoleMenu = function (menu) {
            if ($scope.user.roleCode == 'ROLE_ORGANIZATION_SERVICE') {
                return true;
            }
            if ($scope.user.roleCode == 'ROLE_COMPANY_SERVICE') {
                return checkCompanyService(menu);
            }
            return false;
        }

        var menuListForCompanyServices = new Array("REPORT_MENU", "ACCOUNT_MENU", "LEARNING_MAP_LANGUAGE_MENU_ITEM");
        var checkCompanyService = function (menu) {
            for (var i = 0; i < menuListForCompanyServices.length; i++) {
                if (menu == menuListForCompanyServices[i]) {
                    return false;
                }
            }
            return true;
        }

        //$scope.queryOrgProductList = function(){
        //    var productOptions = ConstantService.getItem("productOptions");
        //    if(StringUtils.isNlOrUndOrEmpty(productOptions)){
        //        //LoadingService.show();
        //        ProductResource.queryListByOrgId({organizationId:$scope.currentUser.organization.id},function(productResult){
        //            $log.debug("组织相关全部产品",productResult);
        //            ConstantService.setItem("productOptions",productResult,true);
        //            $scope.productOptions = $filter("orderBy")(ConstantService.getItem("productOptions"),"-createDate");
        //            //LoadingService.hide();
        //        });
        //    }else{
        //        $scope.productOptions = productOptions;
        //    }
        //};

        //产品列表
        //$scope.productOptions = [];
        //$scope.queryOrgProductList();
    }
);